package com.tzm.gaflow.web.exception;


import com.tzm.gaflow.web.domain.BaseError;
import com.tzm.gaflow.web.domain.ErrorDetail;
import com.tzm.gaflow.web.domain.ErrorType;
import com.tzm.gaflow.web.domain.GlobalError;
import lombok.Getter;
import lombok.Setter;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.Collections;
import java.util.List;

/**
 * 公共业务异常类
 */

public class BusinessException extends RuntimeException {

    private static final long serialVersionUID = 56263647416715478L;

    private Logger logger = LoggerFactory.getLogger(getClass());
    @Getter
    @Setter
    int code;
    @Getter
    @Setter
    String message;
    @Getter
    @Setter
    ErrorType errorType;
    @Getter
    @Setter
    List<ErrorDetail> errorDetails;

    public BusinessException(String message) {
        super(message);
        this.code = GlobalError.INTERNAL_SERVER_ERROR.getCode();
        this.message = message;
        this.errorType = ErrorType.ERROR;
    }

    public BusinessException(String message, Throwable cause) {
        super(message, cause);
        this.code = GlobalError.INTERNAL_SERVER_ERROR.getCode();
        this.message = message;
        this.errorType = ErrorType.ERROR;
    }

    public BusinessException(int code, String message) {
        super(message);
        this.code = code;
        this.message = message;
        this.errorType = ErrorType.ERROR;
    }

    public BusinessException(int code, String message, ErrorType errorType) {
        super(message);
        this.code = code;
        this.message = message;
        this.errorType = errorType;
    }

    public BusinessException(int code, String message, ErrorType errorType, Throwable cause) {
        super(message, cause);
        this.code = code;
        this.message = message;
        this.errorType = errorType;

    }
    public BusinessException(BaseError baseError, List<ErrorDetail> errorDetails) {
        this(baseError.getCode(), baseError.getMessage(), baseError.getType(), errorDetails);
    }


    public BusinessException(BaseError baseError) {
        this(baseError.getCode(), baseError.getMessage(), baseError.getType(), Collections.emptyList());
    }

    public BusinessException(int code, String message, ErrorType errorType, List<ErrorDetail> errorDetails) {
        super(message);
        this.code = code;
        this.message = message;
        this.errorType = errorType;
        this.errorType = ErrorType.ERROR;
        this.errorDetails = errorDetails;
    }

}
